package com.ry.demo.register.client;

import com.ry.demo.register.client.dto.HeartbeatRequest;
import com.ry.demo.register.client.dto.HeartbeatResponse;
import com.ry.demo.register.client.dto.HttpSender;

/**
 * @Author: lihui
 * @Date: 2021-12-01 10:52
 * @Description: 发送心跳
 */
public class HeartBeatWorker implements Runnable {

    /**
     * 服务名称
     */
    private String serviceName;

    /**
     * 服务实例ID
     */
    private String serviceInstanceId;

    /**
     * http请求组件
     */
    private HttpSender httpSender;

    public HeartBeatWorker(String serviceName, String serviceInstanceId, HttpSender httpSender) {
        this.serviceName = serviceName;
        this.serviceInstanceId = serviceInstanceId;
        this.httpSender = httpSender;
    }

    @Override
    public void run() {

        HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
        heartbeatRequest.setServiceName(serviceName);
        heartbeatRequest.setServiceInstanceId(serviceInstanceId);
        HeartbeatResponse heartbeatResponse = null;

        while(true) {
            try {
                heartbeatResponse = httpSender.heartbeat(heartbeatRequest);
                System.out.println("心跳的结果为：" + heartbeatResponse.getStatus() + "......");
                Thread.sleep(30 * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
